package com.yuju.controller.sam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.redis.jfinal.plugin.Redis;
import com.yuju.cache.RedisCache;
import com.yuju.common.Result;
import com.yuju.constant.CacheKey;
import com.yuju.constant.Config;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.enums.SmsTypeEnum;
import com.yuju.enums.VerificationCodeReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.login.model.OpModel;
import com.yuju.login.service.IOpService;
import com.yuju.util.SendSMSUtil;
import com.yuju.util.Utils;
/**
 * 
 * 类名：VerificationCodeController  <br />
 *
 * 功能：验证码
 *
 * @author : zhousc <br />
 * @Date : 2018年1月5日 下午4:29:53  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("pinCode")
public class PinController extends BaseController {
	@Autowired
	private IOpService opService;
	
	@RequestMapping("send")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) throws Exception {
		//接收手机号
		String mobile = obj.getString("mobile");
		//验证码类型 1,登录  2,注册 3,找回密码
		Integer type = obj.getInteger("type");
		
		String key = "";
		//组织缓存键值
		if(type == 1){
			key = mobile + CacheKey.KEY_CACHE_LOGIN_VERIFY_CODE;
		} else if (type == 2) {
			key = mobile + CacheKey.KEY_CACHE_REGISTER_VERIFY_CODE;
		} else if (type ==3) {
			key = mobile + CacheKey.KEY_CACHE_GET_PWD_VERIFY_CODE;
		}
		
		Result result = new Result();
		
		// 检查用户是否已注册
		OpModel opModelParam = new OpModel();
		opModelParam.setName(mobile);
		OpModel opModel = opService.login(opModelParam);
		if (type == 2) {
			if (Utils.isNotNull(opModel)) {
				throw new BusinessException(ReturnCodeEnum.YES_REGISTER.getCode(), ReturnCodeEnum.YES_REGISTER.getDesc());
			}
		} else {
			if (Utils.isNull(opModel)) {
				throw new BusinessException(ReturnCodeEnum.NO_REGISTER.getCode(), ReturnCodeEnum.NO_REGISTER.getDesc());
			}
		}
		
		RedisCache simpleCache = RedisCache.getInstance();
		String cacheVerCode = (String) simpleCache.get(key);
		
		//判断此上次的验证码是否过期
		if(Utils.isNotNull(cacheVerCode)){
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(VerificationCodeReturnCodeEnum.SMS_SEND_ALREADY_SUCCESS.getCode());
			result.setReturnMsg(VerificationCodeReturnCodeEnum.SMS_SEND_ALREADY_SUCCESS.getDesc());
			return result;
		}
		
		//生成验证码
		String randNum = Utils.getRandNum(4);
		SendSmsResponse smsResponse = SendSMSUtil.sendSms(SmsTypeEnum.SMS_TYPE_LOGIN_CODE.getType(), mobile, randNum);
	
		if ("OK".equals(smsResponse.getCode())) {
			//验证码放入缓存
			Redis.use().setex(key, Config.CACHE_VERIFY_CODE_SECONDS, randNum);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(VerificationCodeReturnCodeEnum.SMS_SEND_SUCCESS.getCode());
			result.setReturnMsg(VerificationCodeReturnCodeEnum.SMS_SEND_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(VerificationCodeReturnCodeEnum.SMS_SEND_FAILURE.getCode());
			result.setReturnMsg(VerificationCodeReturnCodeEnum.SMS_SEND_FAILURE.getDesc());
		}
		return result;
	}
	
	
}